package com.xyht.sca_s.student_manage_system.modules.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.SmsDormitory;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.SmsDormitoryBuilding;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.SmsDormitoryFloor;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.req.SmsDormitoryBuildingAddReq;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.req.SmsDormitoryBuildingDeletedReq;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.req.SmsDormitoryBuildingUpdateReq;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.resp.SmsDormitoryBuildingResp;
import com.xyht.sca_s.student_manage_system.modules.dormitory.mapper.SmsDormitoryBuildingMapper;
import com.xyht.sca_s.student_manage_system.modules.dormitory.mapper.SmsDormitoryFloorMapper;
import com.xyht.sca_s.student_manage_system.modules.dormitory.mapper.SmsDormitoryMapper;
import com.xyht.sca_s.student_manage_system.modules.dormitory.service.SmsDormitoryBuildingService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 宿舍楼栋管理表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-01-08
 */
@Service
public class SmsDormitoryBuildingServiceImpl extends ServiceImpl<SmsDormitoryBuildingMapper, SmsDormitoryBuilding> implements SmsDormitoryBuildingService {

    @Resource
    private SmsDormitoryBuildingMapper dormitoryBuildingMapper;
    @Resource
    private SmsDormitoryFloorMapper dormitoryFloorMapper;
    @Resource
    private SmsDormitoryMapper smsDormitoryMapper;

    @Override
    public ResponseResult addDormitoryBuilding(String userId, SmsDormitoryBuildingAddReq addReq) {
        if (isNullOrEmpty(addReq.getBuildingName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        //校验楼栋名重复
        List<SmsDormitoryBuilding> buildingList = dormitoryBuildingMapper.selectList(new LambdaQueryWrapper<SmsDormitoryBuilding>()
                .eq(SmsDormitoryBuilding::getBuildingName,addReq.getBuildingName()));
        if (!buildingList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.DORMITORY_NAME_EXIST);
        }

        SmsDormitoryBuilding smsDormitoryBuilding = new SmsDormitoryBuilding();
        BeanUtils.copyProperties(addReq,smsDormitoryBuilding);
        smsDormitoryBuilding.setUserId(userId);
        dormitoryBuildingMapper.insert(smsDormitoryBuilding);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deletedDormitoryBuilding(SmsDormitoryBuildingDeletedReq deletedReq) {
        if (isNullOrEmpty(deletedReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //楼栋下是否有楼栋
        SmsDormitoryBuilding smsDormitoryBuilding = dormitoryBuildingMapper.selectById(deletedReq.getId());
        if (isNullOrEmpty(smsDormitoryBuilding)) {
            return CommonResult.failed(CommonCodeEnum.DORMITORY_BUILDING_NOT_EXIST);
        }
        //删除宿舍
        smsDormitoryMapper.deleteById(new LambdaQueryWrapper<SmsDormitory>().eq(SmsDormitory::getBuildId, smsDormitoryBuilding.getId()));
        //删除楼层
        dormitoryFloorMapper.delete(new LambdaQueryWrapper<SmsDormitoryFloor>().eq(SmsDormitoryFloor::getBuildingId,deletedReq.getId()));
        //删除楼栋
        dormitoryBuildingMapper.deleteById(deletedReq.getId());

        return CommonResult.success();
    }

    @Override
    public ResponseResult updateDormitoryBuilding(SmsDormitoryBuildingUpdateReq updateReq) {
        if (isNullOrEmpty(updateReq.getId(),updateReq.getBuildingName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsDormitoryBuilding smsDormitoryBuilding = dormitoryBuildingMapper.selectById(updateReq.getId());
        if (isNullOrEmpty(smsDormitoryBuilding)) {
            return CommonResult.failed(CommonCodeEnum.DORMITORY_BUILDING_NOT_EXIST);
        }

        //校验楼栋名重复
        List<SmsDormitoryBuilding> buildingList = dormitoryBuildingMapper.selectList(new LambdaQueryWrapper<SmsDormitoryBuilding>()
                .eq(SmsDormitoryBuilding::getBuildingName, updateReq.getBuildingName())
                .ne(SmsDormitoryBuilding::getId, updateReq.getId()));
        if (!buildingList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.DORMITORY_NAME_EXIST);
        }


        BeanUtils.copyProperties(updateReq,smsDormitoryBuilding);
        dormitoryBuildingMapper.updateById(smsDormitoryBuilding);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getDormitoryBuildingList(Integer pageNum, Integer pageSize, String name) {
        Page<SmsDormitoryBuilding> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsDormitoryBuilding> dormitoryBuildingLambdaQueryWrapper = new LambdaQueryWrapper<SmsDormitoryBuilding>()
                .orderByAsc(SmsDormitoryBuilding::getBuildingName);
        if (!isNullOrEmpty(name)) {
            dormitoryBuildingLambdaQueryWrapper.like(SmsDormitoryBuilding::getBuildingName,name);
        }

        dormitoryBuildingMapper.selectPage(page,dormitoryBuildingLambdaQueryWrapper);
        List<SmsDormitoryBuildingResp> dormitoryBuildingList = page.getRecords()
                .stream()
                .map(smsDormitoryBuilding -> {
                    SmsDormitoryBuildingResp smsDormitoryBuildingResp = new SmsDormitoryBuildingResp();
                    BeanUtils.copyProperties(smsDormitoryBuilding,smsDormitoryBuildingResp);
                    List<SmsDormitoryFloor> dormitoryFloorList = dormitoryFloorMapper.selectList(new LambdaQueryWrapper<SmsDormitoryFloor>().
                            eq(SmsDormitoryFloor::getBuildingId, smsDormitoryBuilding.getId()));
                    if (dormitoryFloorList.isEmpty()) {
                        smsDormitoryBuildingResp.setFlag(0);//没有设置楼层
                    } else {
                        smsDormitoryBuildingResp.setFlag(1);
                    }
                    return smsDormitoryBuildingResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(dormitoryBuildingList,total);
    }
}
